<?php
!defined ('P_W') && exit('Forbidden');
class PW_HouseImage {
	
	function __construct() {
		//$this->_init();
	}
	function PW_HouseImage() {
		$this->__construct();
	}
	
	function addImages($hid,$typeid,$creator,$descrip) {
		global $timestamp,$db_charset;
		$hid = (int) $hid;
		$typeid = (int) $typeid;
		$images = $this->_uploadPicture($_FILES, $hid,$typeid,$creator);
		if (!$images) return false; 
		$imageDB = $this->_getImageDB();
		$imageDataDB = $this->_getImageDataDB();
		foreach ($images as $key=>$value) {
			$descrip = pwConvert($descrip, $db_charset, 'utf-8');
			$temp = $imageDB->insert(array(
				'typeid'=>$typeid,
				'hid'	=>$hid,
				'size'	=>$value['size'],
				'path'	=>$value['fileuploadurl'],
				'uploadtime'	=>$timestamp,
				'descrip'	=>$descrip,
				'creator'	=>$creator,
			));
			if (!$temp) Showmsg('undefined_action');
			$temp_1 = $imageDataDB->insert(array('imageid'=>$temp,'hits'=>0));
			if (!$temp_1) Showmsg('undefined_action');
		}
		$this->_updateCache($hid);
		return true;
	}
	
	function getImages($hid,$typeid=0,$page=1) {
		$imageDB = $this->_getImageDB();
		$hid = (int) $hid;
		if ($hid) {
			$temp = $imageDB->getImages($hid,$typeid,$page);
		} else {
			$temp = $imageDB->getImagesByTypeId($typeid,$page);
		}
		foreach ($temp as $id=>$image) {
			$temp[$id] = $this->_initData($image);
		}
		return $temp;
	}
	
	function searchImages($data,$page) {
		$imageDB = $this->_getImageDB();
		$temp = $imageDB->searchImages($data,$page);
		
		foreach ($temp as $id=>$image) {
			$temp[$id] = $this->_initData($image);
		}
		return $temp;
	}
	
	function countSearchImages($data) {
		$imageDB = $this->_getImageDB();
		return $count = $imageDB->countSearchImages($data);
	}
	
	function getSearchImagesPages($data,$page,$url) {
		$imageDB = $this->_getImageDB();
		$total = $imageDB->countSearchImages($data);
		
		$numofpage = ceil($total/$imageDB->_perpage);
		$numofpage < 1 && $numofpage = 1;
		$page > $numofpage && $page = $numofpage;
		//$url = $this->_initSearchUrl($url,$data);
		return numofpage($total,$page,$numofpage,$url);
	}
	
	function getPages($hid,$typeid,&$page,$url) {
		$imageDB = $this->_getImageDB();
		$total = $imageDB->countForHouse($hid,$typeid);
		$perpage = $imageDB->_perpage;
		$numofpage = ceil($total/$perpage);
		$numofpage < 1 && $numofpage = 1;
		$page > $numofpage && $page = $numofpage;
		return numofpage($total,$page,$numofpage,$url);
	}
	
	function updateImage($id,$data) {
		$imageDB = $this->_getImageDB();
		$image = $imageDB->get($id);
		$this->_updateCache($image['hid']);
		$imageDB->update($id,$data);
	}
	
	function deleteImage($id) {
		$imageDB = $this->_getImageDB();
		$image = $imageDB->get($id);
		if ($image) {
			global $attachdir;
			P_unlink($attachdir.'/'.$image['path']);
			P_unlink($this->_getMiniImageUrl($attachdir.'/'.$image['path']));
			$imageDB->delete($id);
		}
		$this->_updateCache($image['hid']);
		$imageDataDB = $this->_getImageDataDB();
		$imageDataDB->delete($id);
	}
	
	function deleteImages($ids) {
		foreach ($ids as $value) {
			$this->deleteImage($value);
		}
	}
	
	function deleteImagesByTypeId($typeId) {
		$images = $this->getImages(0,$typeId,'all');
		$ids = array_keys($images);
		$this->deleteImages($ids);
	}
	
	function getImage($id) {
		$imageDB = $this->_getImageDB();
		$temp = $imageDB->get($id);
		if (!$temp) return null;
		return $this->_initData($temp);
	}
	
	function getFlash($winduid) {
		global $pwServer;
		require_once(M_P . 'lib/model/imageupload.class.php');
		$uploadfiletype = ImageUpload::getFtype();
		foreach ($uploadfiletype as $key => $value) {
			$filetype .= ($filetype ? ',' : '') . $key . ':' . $value;
		}
		$pwServer['HTTP_USER_AGENT'] = 'Shockwave Flash';
		$swfhash = GetVerify($winduid);
	
		return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><swf><filetype>{$filetype}</filetype><uid>{$winduid}</uid><step>2</step><verify>{$swfhash}</verify></swf>";
	}
	
	function setPerpage($num) {
		$imageDB = $this->_getImageDB();
		$imageDB->setPerpage($num);
	}
	
	function _updateCache($hid) {
		$pageCache = getPageCache('info',$hid);
		$temp_config = $pageCache->config;
		foreach ($temp_config as $key=>$value) {
			if ($value['type']=='imagetype') {
				$pageCache->deleteCache($key);
			}
		}
	}
	
	function _initData($data) {
		global $houseBaseUrl;
		$data['url'] = $houseBaseUrl.'q=image&id='.$data['id'];
		$data['path'] = $this->_getImageUrl($data['path']);
		$data['mini'] = $this->_getMiniImageUrl($data['path']);
		!file_exists($data['mini']) && $data['mini'] = $data['path'];
		$data['uploadtime'] = get_date($data['uploadtime'],'Y-m-d H:i');
		return $data;
	}
	
	function _getImageUrl($imageUrl) {
		$temp = geturl($imageUrl);
		if (!$temp) return false;
		return $temp[0];
	}
	function _getMiniImageUrl($imageUrl) {
		return str_replace($this->_getNaturePath(),$this->_getMiniPath(),$imageUrl);
	}
	
	function _uploadPicture($fileArray,$hid,$typeid,$creator) {
		require_once(M_P . 'lib/model/imageupload.class.php');
		$img = new ImageUpload($hid,$this->_getNaturePath(),$this->_getMiniPath());
		PwUpload::upload($img);
		return $img->getAttachs();
	}
	
	function _getImageDB() {
		return House::loadDB('house_image');
	}
	function _getImageDataDB() {
		return House::loadDB('house_imagedata');
	}
	function _getNaturePath() {
		return 'house/nature/';
	}
	function _getMiniPath() {
		return 'house/mini/';
	}

}
?>